﻿namespace WMS6.BLL
{
    using System;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class UserPreferenceBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly IPreferenceProfileDA preferenceProfileDA = DataAccess.CreatePreferenceProfileDA();
        private static readonly IPreferenceProfileDetailDA preferenceProfileDetailDA = DataAccess.CreatePreferenceProfileDetailDA();

        public PreferenceProfileInfo GetPreferenceProfileByUserID(string preferenceID)
        {
            PreferenceProfileInfo preferenceProfile = preferenceProfileDA.GetPreferenceProfileByUserID(preferenceID);
            if (preferenceProfile != null)
            {
                preferenceProfile.PreferenceProfileDetailInfos = preferenceProfileDetailDA.GetPreferenceProfileDetailByID(preferenceProfile.PreferenceProfileID);
                return preferenceProfile;
            }
            preferenceID = "Default";
            preferenceProfile = preferenceProfileDA.GetPreferenceProfileByUserID(preferenceID);
            preferenceProfile.PreferenceProfileDetailInfos = preferenceProfileDetailDA.GetPreferenceProfileDetailByID(preferenceProfile.PreferenceProfileID);
            return preferenceProfile;
        }

        public string InsertUserPreference(PreferenceProfileInfo preferencePrifile)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                preferenceProfileDetailDA.DeletePreferenceProfileDetail(dataBase, tran, preferencePrifile.PreferenceProfileID);
                preferenceProfileDA.DeletePreferenceProfile(dataBase, tran, preferencePrifile.PreferenceProfileID);
                if (!preferencePrifile.IsReset)
                {
                    string digitNo = string.Empty;
                    preferenceProfileDA.InsertPreferenceProfile(dataBase, tran, preferencePrifile);
                    foreach (PreferenceProfileDetailInfo info in preferencePrifile.PreferenceProfileDetailInfos)
                    {
                        preferenceProfileDetailDA.InsertPreferenceProfileDetail(dataBase, tran, info);
                        if (info.PreferenceID.ToUpper() == "DIGITS_NO")
                        {
                            digitNo = info.PreferenceValue;
                        }
                    }
                    preferenceProfileDetailDA.UpdateAllPreferenceProfileInfoByID(dataBase, tran, "DIGITS_NO", digitNo, preferencePrifile.UpdatedBy);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return preferencePrifile.UserID;
        }
    }
}

